<template>
  <div>
    <common-head title="分类" />
    <div class="cate">
      <div class="left">
        <van-sidebar v-model="activeKey" @change="onChange">
          <van-sidebar-item
            v-for="cate in cates"
            :key="cate.id"
            :title="cate.name"
          />
        </van-sidebar>
      </div>
      <div class="right">
        <div class="items">
          <div
            class="item"
            v-for="item in items"
            :key="item.id"
            @click="clickItem(item.id)"
          >
            <img :src="item.pic" class="img" alt="" />
            <div class="item-info">
              <h5 class="name">{{ item.name }}</h5>
              <p class="desc">
                {{ item.characteristic ? item.characteristic : "暂无描述" }}
              </p>
              <div class="acion">
                <p class="price">￥{{ item.minPrice }}</p>
                <van-icon
                  @click.stop="addCart(item.id)"
                  size="25"
                  class-prefix="icon"
                  name="shop"
                />
              </div>
            </div>
          </div>
        </div>
        <van-empty
          v-show="empty"
          class="custom-image"
          image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
          description="靓仔这里什么都没有"
        />
      </div>
    </div>
    <common-tab />
  </div>
</template>

<script>
import CommonTab from 'components/CommonTab'
import CommonHead from 'components/CommonHead'
import { isLogin, getToken } from 'utils'
import { fetchCates, fetchItems, addCart } from 'api'
import { Toast } from 'vant'
export default {
  name: 'cate',
  data () {
    return {
      cates: [],
      items: [],
      num: 0,
      activeKey: 0,
      empty: false
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    clickItem (id) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    addCart (id) {
      console.log(id)
      // 加入购物车的动作 需要登录才能访问
      if (!isLogin()) {
        // 没有登录
        Toast.fail({
          message: '请登录',
          duration: 1000,
          onClose: () => {
            // 跳转到登录页
            this.$router.push({
              path: '/login',
              query: {
                from: '/home'
              }
            })
          }
        })
        return false
      }
      // 登录了 加入购物车
      addCart({
        goodsId: id,
        number: 1,
        token: getToken()
      }).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          Toast.success('加入成功，请前往购买')
          this.$store.commit('cart/set_items', res.data.data.items)
        }
      })
    },
    onChange (index) {
      this.num = index
      this.fetchItems()
    },
    fetchItems () {
      this.items = []
      fetchItems({
        categoryId: this.cates[this.num].id
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
          console.log(this.items)
        }
        if (res.data.code === 700) {
          this.empty = true
        } else {
          this.empty = false
        }
      })
    },
    fetchCates () {
      fetchCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    }
  },
  components: {
    CommonTab,
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.van-sidebar-item--select::before {
  background-color: #ff734c;
}

.cate {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 50px;
  display: flex;
  .left {
    position: fixed;
    padding: 46px 0;
  }
  .right {
    overflow-y: auto;
    flex: 1;
    padding-left: 80px;
  }
}

.items {
  .item {
    height: 230px;
    margin: 8px 0;
    display: flex;
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden;
    align-items: center;
    .img {
      width: 160px;
      height: 160px;
    }
    .item-info {
      flex: 1;
      height: 100%;
      padding-top: 20px;
      padding-left: 5px;
      .name {
        font-size: 16px;
        font-weight: 600;
        color: #333;
        margin-top: 10px;
        line-height: 20px;
      }
      .desc {
        width: 100%;
        font-size: 12px;
        color: #666;
        margin-top: 20px;
        line-height: 20px;
      }
      .acion {
        display: flex;
        margin-top: 60px;
        padding-right: 10px;
        height: 50px;
        align-items: center;
        justify-content: space-between;
        font-size: 16px;
        color: #ff734c;
        .price {
          font-size: 16px;
          font-weight: 600;
          color: #232628;
        }
      }
    }
  }
}
.custom-image {
  height: 100%;
}
</style>
